package com.net.job;

import java.util.Iterator;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.net.model.UserFlow;
import com.net.model.UserFlowStatData;
import com.net.model.WebsiteFlow;
import com.net.model.WebsiteFlowStatData;
import com.net.util.SpringUtils;

@Component
public class UpdateCursorJob {
	public void updateCursor() {
		System.out.println("UpdateCursorJob executed!");
		WebsiteFlowStatData websiteFlowStatData = SpringUtils.getBean("website_flow_data");
		UserFlowStatData userFlowStatData = SpringUtils.getBean("user_flow_data");
		
		System.out.println("执行前用户数据:" + userFlowStatData);
		System.out.println("执行前网站数据:" + websiteFlowStatData);
		Map<String, WebsiteFlow> websiteData = websiteFlowStatData.getData();
		Iterator iter = websiteData.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			WebsiteFlow val = (WebsiteFlow) entry.getValue();
			int m = val.getCursor() + 1;
			Double[] flowPerMinute = val.getFlowPerMinute();
			if(m > 29){
				m = 0;
			}
			flowPerMinute[m] = 0.0;
			val.setFlowPerMinute(flowPerMinute);
			val.setCursor(m);
			websiteData.put(key, val);
		}
		websiteFlowStatData.setData(websiteData);
		
		Map<String, UserFlow> userData = userFlowStatData.getData();
		iter = userData.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			UserFlow val = (UserFlow) entry.getValue();
			int m = val.getCursor() + 1;
			Double[] flowPerMinute = val.getFlowPerMinute();
			if(m > 29){
				m = 0;
			}
			flowPerMinute[m] = 0.0;
			val.setFlowPerMinute(flowPerMinute);
			val.setCursor(m);
			userData.put(key, val);
		}		
		userFlowStatData.setData(userData);
		
		 websiteFlowStatData = SpringUtils.getBean("website_flow_data");
		 userFlowStatData = SpringUtils.getBean("user_flow_data");
		System.out.println("执行后用户数据:" + userFlowStatData);
		System.out.println("执行后网站数据:" + websiteFlowStatData);
	}
}
